package com.fly.feishu.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.fly.feishu.domain.service.EmployeeService;
import com.fly.feishu.infrastructure.persistent.po.Employee;
import com.fly.feishu.infrastructure.utils.MD5Util;
import com.fly.feishu.web.config.LoginIntercept;
import com.fly.feishu.web.constant.FeishuWebConstant;
import com.fly.feishu.web.model.response.FeishuResponse;
import com.fly.feishu.web.utils.CookieUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.impl.DefaultClaims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登陆页面
     *
     * @param modelAndView
     * @return
     */
    @GetMapping("/login")
    public ModelAndView loginPage(ModelAndView modelAndView) {
        modelAndView.setViewName("login");
        return modelAndView;
    }

    /**
     * 登陆页面
     *
     * @param modelAndView
     * @return
     */
    @GetMapping("/login/feishuLoginPage")
    public ModelAndView feishuLoginPage(ModelAndView modelAndView) {
        modelAndView.setViewName("redirect:" + FeishuWebConstant.LOGIN_URL_FEISHU);
        return modelAndView;
    }

    /**
     * 登陆请求处理
     *
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public FeishuResponse login(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response) {
        Employee employee = employeeService.findByUnique(username);
        if (employee == null) {
            return FeishuResponse.error(401, "用户信息不存在");
        }
        String passwordMd5 = MD5Util.getMD5(password);
        if (!StringUtils.equals(passwordMd5, employee.getPassword())) {
            return FeishuResponse.error(401, "密码不正确");
        }
        Claims claims = new DefaultClaims();
        claims.setId(employee.getId().toString()).setSubject(JSONObject.toJSONString(employee));
        LoginIntercept.refreshLocalToken(claims, request, response);

        return FeishuResponse.ok("登陆成功");
    }

    /**
     * 注销
     * @param request
     * @param response
     * @param modelAndView
     * @return
     */
    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        modelAndView.setViewName("login");
        CookieUtils.clearCookie(request, response, FeishuWebConstant.TOKEN_NAME);
        CookieUtils.clearCookie(request, response, FeishuWebConstant.FEISHU_TOKEN_NAME);
        return modelAndView;
    }
}
